package cn.cloudplug.friendsrecommend.view;


import cn.cloudplug.friendsrecommend.R;
import cn.cloudplug.friendsrecommend.SearchRaceActivity;
import cn.cloudplug.friendsrecommend.util.CalendarUtil;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class RightMenu implements View.OnClickListener,OnTouchListener{
	private ViewFlipper flipper = null;
	/** 每次添加gridview到viewflipper中时给的标记 */
	private int gvFlag = 0;
	/** 当前的年月，现在日历顶端 */
	private TextView currentMonth;
	private TextView tv_search;
	/** 上个月 */
	private ImageView prevMonth;
	/** 下个月 */
	private ImageView nextMonth;

	private CalendarUtil calendarUtil;


	
	private Activity activity;
	
	private SlidingMenu slidingMenu;
	
	public RightMenu(final Activity activity) {
		// TODO Auto-generated constructor stub
		this.activity = activity;

		
		
		
		slidingMenu = new SlidingMenu(activity);
		
		
		View view = View.inflate(activity, R.layout.calendar, null);
		
		slidingMenu.setMenu(view);
		slidingMenu.attachToActivity(activity, SlidingMenu.SLIDING_CONTENT);
		slidingMenu.setMode(SlidingMenu.RIGHT);
		int screenWidth = activity.getWindowManager().getDefaultDisplay()
				.getWidth();
		slidingMenu.setBehindOffset(screenWidth *1 / 5);
		tv_search=(TextView) view.findViewById(R.id.tv_search);
		currentMonth = (TextView) view.findViewById(R.id.currentMonth);
		prevMonth = (ImageView)  view.findViewById(R.id.prevMonth);
		nextMonth = (ImageView)  view.findViewById(R.id.nextMonth);

		flipper = (ViewFlipper) view.findViewById(R.id.flipper);
		RelativeLayout rl=(RelativeLayout) view.findViewById(R.id.relativelayout);
		
		calendarUtil=new CalendarUtil(screenWidth*4/5,activity,flipper,currentMonth,rl);
		calendarUtil.loadCurrentCalendar();
		
		view.setOnTouchListener(this);
		setListener();
	}
	
	
	
	
	private void setListener() {
		tv_search.setOnClickListener(this);
		prevMonth.setOnClickListener(this);
		nextMonth.setOnClickListener(this);
		currentMonth.setOnClickListener(this);
		
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.nextMonth: // 下一个月
			calendarUtil.hide();
			calendarUtil.enterNextMonth(gvFlag);
			break;
		case R.id.tv_search: 
			calendarUtil.hide();
			activity.startActivity(new Intent(activity, SearchRaceActivity.class));
			break;
		case R.id.prevMonth: // 上一个月
			calendarUtil.hide();
			calendarUtil.enterPrevMonth(gvFlag);
			break;
		case R.id.currentMonth:
			calendarUtil.hide();
			DatePickerDialog dpd=new DatePickerDialog(activity, new OnDateSetListener() {
				@Override
				public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) {
					// TODO Auto-generated method stub
					calendarUtil.setYear_c(arg1);
					calendarUtil.setMonth_c(arg2+1);
					calendarUtil.setDay_c(arg3);
					calendarUtil.loadCurrentCalendar();
				}
			}, calendarUtil.getYear_c(), calendarUtil.getMonth_c()-1, calendarUtil.getDay_c());
			dpd.show();
		}
	}
	public void showMenu() {
		slidingMenu.showMenu();
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(calendarUtil.tv!=null){
			calendarUtil.hide();
			return false;
		}
		return false;
	}

}
